<template>
  <!-- 交易记录表格 -->
  <el-card shadow="hover" style="margin-top: 20px;">
    <template #header>
      <div class="card-header">
        <span>交易记录</span>
      </div>
    </template>
    <el-table :data="backtestTrades" style="width: 100%;" stripe>
      <el-table-column prop="date" label="日期" width="120" sortable />
      <el-table-column prop="type" label="类型" width="80">
        <template #default="scope">
          <el-tag :type="scope.row.type === 'buy' ? 'success' : 'danger'" size="small">
            {{ scope.row.type === 'buy' ? '买入' : '卖出' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120">
        <template #default="scope">
          ${{ scope.row.price.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="amount" label="数量" width="120" />
      <el-table-column prop="value" label="价值" width="120">
        <template #default="scope">
          ${{ scope.row.value.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column prop="profit" label="盈亏" width="120" v-if="backtestTrades.some(t => t.profit !== undefined)">
        <template #default="scope">
          <span v-if="scope.row.profit !== undefined" 
                :class="scope.row.profit >= 0 ? 'profit' : 'loss'">
            ${{ scope.row.profit.toFixed(2) }}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
export default {
  name: 'TradeRecords',
  props: {
    backtestTrades: {
      type: Array,
      required: true
    }
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profit {
  color: #14b143;
}

.loss {
  color: #ef232a;
}
</style>